/**
 * Copyright &copy; 2015-2020 <a href="www.dsesa.cn/">ESA</a> All rights reserved.
 */
package com.jeeplus.common.utils.excel;

import com.jeeplus.common.Excel2PdfUtil;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.FileUtils;
import com.jeeplus.common.utils.StringUtils;
import net.sf.jxls.exception.ParsePropertyException;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 导出Excel文件（导出“XLSX”格式，支持大数据量导出   @see org.apache.poi.ss.SpreadsheetVersion）
 * @author jeeplus
 * @version 2016-04-21
 */
public class ExportExcelNew {

	private static Logger log = LoggerFactory.getLogger(ExportExcelNew.class);

	/**
	 * 根据模板生成Excel文件.
	 */
	public String createExcel(String srcFilePath, Map map){
		//创建XrLSTransformer对象
		XLSTransformer transformer = new XLSTransformer();
		//文件名
		String fileName = "";
		String destFilePath = "";
		String pdfName = "";
		FileUtils.createDirectory(Excel2PdfUtil.excelPath);
		FileUtils.createDirectory(Excel2PdfUtil.pdfPath);
		FileUtils.createDirectory(Excel2PdfUtil.hc);
		try {
			Map<String,Object> beanParams = new HashMap<String,Object>();
			if(map != null && !map.isEmpty()){
				Iterator it = map.entrySet().iterator();
				while(it.hasNext()){
					Map.Entry entry = (Map.Entry) it.next();
					String bean = (String)entry.getKey();
					List list = (List)entry.getValue();
					if(bean.endsWith("List")){
							beanParams.put(bean, list);
					}else {
						for (int i = 0;i<list.size();i++){
							String s = (i+1)+"";
							beanParams.put(bean+s, list.get(i));
						}
					}
				}
				DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				beanParams.put("printTime", df.format(new Date()));
				//生成Excel文件
				fileName = DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
				destFilePath = Excel2PdfUtil.excelPath+fileName;
				transformer.transformXLS(srcFilePath, beanParams, destFilePath);
				Excel2PdfUtil.excel2pdf(fileName);
				pdfName = Excel2PdfUtil.mergePdf();

			}


		} catch (ParsePropertyException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		}finally {
			return pdfName;
		}
	}

	/**
	 * 根据模板生成Excel文件.
	 */
	public String createExcel1(String srcFilePath, List<Map> list){
		//创建XLSTransformer对象
		XLSTransformer transformer = new XLSTransformer();
		//文件名
		String fileName = "";
		String destFilePath = "";
		String pdfName = "";
		FileUtils.createDirectory(Excel2PdfUtil.excelPath);
		FileUtils.createDirectory(Excel2PdfUtil.pdfPath);
		FileUtils.createDirectory(Excel2PdfUtil.hc);
		try {
			Map<String,Object> beanParams = new HashMap<String,Object>();
			if(list != null && !list.isEmpty()){
				int w = 0;
				for(Map map : list){
					beanParams.put("bean", map);
					//生成Excel文件
					fileName = DateUtils.getDate("yyyyMMddHHmmss") + w + ".xlsx";
					destFilePath = Excel2PdfUtil.excelPath+fileName;
					transformer.transformXLS(srcFilePath, beanParams, destFilePath);
					Excel2PdfUtil.excel2pdf(fileName);
					w++;
				}
				System.out.println("打印张数："+w);
				pdfName = Excel2PdfUtil.mergePdf();
			}

		} catch (ParsePropertyException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		}finally {
			return pdfName;
		}
	}


}
